Skip to main content

Data shaping ADSI and Exchange WMI information

I blogged last week about relating mailbox size data with mailbox quota data here. The main problem I had with the approach that I was using here is although it worked fine for 1 user when you wanted to relate a lot of data say the size of everybody’s mailbox on one particular server to the possible size of the mailbox quotas from either their account or mailbox store it gets a little slow.

Data shaping is an ADO thing that allows you to build hierarchical relationships between two or more logical entities in a query. What this means is that I can take two or more separate data sources (via a disconnected recordset) and bung them together in a hierarchical tree (as long as you have a field that relates between the two sources).

The guys over at Rolla do a lot better job of explaining it them me have a look at http://www.4guysfromrolla.com/webtech/060301-1.shtml and
http://www.4guysfromrolla.com/webtech/100699-2.shtml

In my particular scenario I want to shape data from 3 different data sources
1. All the mail store objects for a particular server from the Configuration partition in AD – via ADSI
2. All the visible mailboxes (AD- User object) on a server – Via ADIS
3. All the mailbox sizes – Via Exchange_Mailbox WMI on Exchange 2003

Essentially the hierarchy I wanted to create would look like this

Mail Store Quotas -+
------------------------+---User Account Quotas -+
---------------------------------------------------------+--- Mailbox Size

Because the providers I’m using aren’t going to support the Shape command directly using offline recordsets provides the best method of putting the data into a format that can be shaped.

So what the script does is takes the server-name as a command line parameter and first does a query of Active Directory to get the full distinguished Name of the server. This is then used to find all the mail-stores that belong to this particular server. The information about the mail-store quotas of each of the mail-stores on this server becomes the parent record-set. The next ADSI query that is run is a query of the visible mailboxes in the GAL that are on this server. This information then becomes the child record-set and relates to the parent record-set based on the HomeMDB property of the user object and the DN or the mailstore. The last query to be performed is a grab of all the mailbox sizes on the server from the Exchange_Mailbox WMI class. This information becomes the grandchild record-set and relates to the child recod-set though the legancyDN property of the WMI instance and the AD legacyExchangeDN property of the user object.

The output of the code is all piped to a csv file that by default will be saved to the root of the c:\ drive. Some quick math is done of the mailbox size and quotas size to work out the percentage of the quota that is being used.

In its script form I’m not sure how useful this code is, I hope to turn it into a little Asp.net app that a helpdesk person could use to manage the quota’s for all user's on a server. The other approach maybe to filter the records that are returned further so say you only look at people who are currently at 90 % of there quota and then send a mail to your helpdesk operators to chase up etc.

I’ve posted a download copy of the code here

The script looks like

servername = wscript.arguments(0)
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
set conn1 = createobject("ADODB.Connection")
strConnString = "Data Provider=NONE; Provider=MSDataShape"
conn1.Open strConnString
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("configurationNamingContext")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\exportquotas.csv",2,true)
wfile.writeline("Username,StorageQuoata(MB),OverLimitQuota(MB),HardLimitQuota(MB),Mailbox
Size(MB),Percent Used")
set objParentRS = createobject("adodb.recordset")
set objChildRS = createobject("adodb.recordset")
strSQL = "SHAPE APPEND" & _
" NEW adVarChar(255) AS SOADDisplayName, " & _
" NEW adVarChar(255) AS SOADDistName, " & _
" NEW adVarChar(255) AS SOmDBStorageQuota, " & _
" NEW adVarChar(255) AS SOmDBOverQuotaLimit, " & _
" NEW adVarChar(255) AS SOmDBOverHardQuotaLimit, " & _
" ((SHAPE APPEND " & _
" NEW adVarChar(255) AS MOADDisplayName, " & _
" NEW adVarChar(255) AS MOADLegacyDN, " & _
" NEW adVarChar(255) AS MOADHomeMDB, " & _
" NEW adBoolean AS MOmDBUseDefaults, " & _
" NEW adVarChar(255) AS MOmDBStorageQuota, " & _
" NEW adVarChar(255) AS MOmDBOverQuotaLimit, " & _
" NEW adVarChar(255) AS MOmDBOverHardQuotaLimit, " & _
" ((SHAPE APPEND " & _
" NEW adVarChar(255) AS WMILegacyDN, " & _
" NEW adVarChar(255) AS WMISize, " & _
" NEW adVarChar(255) AS WMIStorename) " & _
" RELATE MOADLegacyDN TO WMILegacyDN) AS MOWMI" & _
")" & _
" RELATE SOADDistName TO MOADHomeMDB) AS rsSOMO "
objParentRS.LockType = 3
objParentRS.Open strSQL, conn1
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
svcQuery = "<LDAP://" & strNameingContext & ">;(&(objectCategory=msExchExchangeServer)(cn="
& Servername & "));cn,name,distinguishedName,legacyExchangeDN;subtree"
Com.ActiveConnection = Conn
Com.CommandText = svcQuery
Set Rs = Com.Execute
while not rs.eof
sgQuery = "<LDAP://" & strNameingContext & ">;(&(objectCategory=msExchPrivateMDB)(msExchOwningServer="
& rs.fields("distinguishedName") & "));cn,name,displayname,distinguishedName,
mDBStorageQuota,mDBOverQuotaLimit,mDBOverHardQuotaLimit,
legacyExchangeDN;subtree"
Com.CommandText = sgQuery
Set Rs1 = Com.Execute
while not rs1.eof
objParentRS.addnew
objParentRS("SOADDisplayName") = rs1.fields("displayname")
objParentRS("SOADDistName") = left(rs1.fields("distinguishedName"),255)
objParentRS("SOmDBStorageQuota") = rs1.fields("mDBStorageQuota")
objParentRS("SOmDBOverQuotaLimit") = rs1.fields("mDBOverQuotaLimit")
objParentRS("SOmDBOverHardQuotaLimit") = rs1.fields("mDBOverHardQuotaLimit")
objParentRS.update
rs1.movenext
wend
wscript.echo "finished 1st AD query storage groups"
GALQueryFilter = "(&(&(&(& (mailnickname=*)(!msExchHideFromAddressLists=TRUE)(|
(&(objectCategory=person)(objectClass=user)(msExchHomeServerName=" &
rs.fields("legacyExchangeDN") & ")) )))))"
strQuery = "<LDAP://" & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,msExchMailboxGuid,displayname,
mDBStorageQuota,mDBOverQuotaLimit,mDBOverHardQuotaLimit
,mDBUseDefaults,msExchHomeServerName,legacyExchangeDN,homemdb;subtree"
com.Properties("Page Size") = 100
Com.CommandText = strQuery
Set Rs2 = Com.Execute
objChildRS.LockType = 3
Set objChildRS = objParentRS("rsSOMO").Value
while not rs2.eof
objChildRS.addnew
objChildRS("MOADDisplayName") = rs2.fields("displayname")
objChildRS("MOADLegacyDN") = rs2.fields("legacyExchangeDN")
objChildRS("MOADHomeMDB") = left(rs2.fields("homemdb"),255)
objChildRS("MOmDBUseDefaults") = rs2.fields("mDBUseDefaults")
objChildRS("MOmDBStorageQuota") = rs2.fields("mDBStorageQuota")
objChildRS("MOmDBOverQuotaLimit") = rs2.fields("mDBOverQuotaLimit")
objChildRS("MOmDBOverHardQuotaLimit") = rs2.fields("mDBOverHardQuotaLimit")
objChildRS.update
rs2.movenext
wend
wscript.echo "finished 2nd AD query mailbox"
rs.movenext
wend
Set objgrandchild = objChildRS("MOWMI").Value
strWinMgmts ="winmgmts:{impersonationLevel=impersonate}!//"& servername
&"/root/MicrosoftExchangeV2"
Set objWMIExchange = GetObject(strWinMgmts)
Set listExchange_MailboxSizes = objWMIExchange.ExecQuery("Select * FROM
Exchange_Mailbox",,48)
For each objExchange_MailboxSize in listExchange_MailboxSizes
objgrandchild.addnew
objgrandchild("WMILegacyDN") = objExchange_MailboxSize.LegacyDN
objgrandchild("WMISize") = objExchange_MailboxSize.size
objgrandchild("WMIStorename") = objExchange_MailboxSize.storename
objgrandchild.update
Next
wscript.echo "finished 3nd Exchange WMI query"
objParentRS.MoveFirst
Do While Not objParentRS.EOF
Set objChildRS = objParentRS("rsSOMO").Value
strQuota = objParentRS("SOmDBStorageQuota")
if isnull(objParentRS("SOmDBStorageQuota")) then strQuota = 0
strQuotaLimit = objParentRS("SOmDBOverQuotaLimit")
if isnull(objParentRS("SOmDBOverQuotaLimit")) then strQuotaLimit = 0
strHardQuotaLimit = objParentRS("SOmDBOverHardQuotaLimit")
if isnull(objParentRS("SOmDBOverHardQuotaLimit")) then strHardQuotaLimit = 0
Do While Not objChildRS.EOF
strUsername = replace(objChildRS("MOADDisplayName"),",","")
strUsedefaults = objChildRS("MOmDBUseDefaults")
strusrQuota = objChildRS("MOmDBStorageQuota")
if isnull(objChildRS("MOmDBStorageQuota")) then strusrQuota = 0
strusrQuotaLimit = objChildRS("MOmDBOverQuotaLimit")
if isnull(objChildRS("MOmDBOverQuotaLimit")) then strusrQuotaLimit = 0
strusrHardQuotaLimit = objChildRS("MOmDBOverHardQuotaLimit")
if isnull(objChildRS("MOmDBOverHardQuotaLimit")) then strusrHardQuotaLimit = 0
Set objgrandchild = objChildRS("MOWMI").Value
Do While Not objgrandchild.EOF
mbsize = objgrandchild("WMISize")
if strUsedefaults = false then
if strusrQuota <> 0 then
wfile.writeline strUsername & "," &
replace(formatnumber(strusrQuota/1024,2),",","") & "," &
replace(formatnumber(strusrQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strusrHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," &
formatnumber((mbsize/strusrQuota)*100,2) & "%"
else
wfile.writeline strUsername & "," &
replace(formatnumber(strusrQuota/1024,2),",","") & "," &
replace(formatnumber(strusrQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strusrHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," & 0 & "%"
end if
else
if strQuota <> 0 then
wfile.writeline strUsername & "," &
replace(formatnumber(strQuota/1024,2),",","") & "," &
replace(formatnumber(strQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," &
formatnumber((mbsize/strQuota)*100,2) & "%"
else
wfile.writeline strUsername & "," &
replace(formatnumber(strQuota/1024,2),",","") & "," &
replace(formatnumber(strQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," & 0 & "%"
end if
end if
objgrandchild.MoveNext
Loop
objChildRS.MoveNext
Loop
objParentRS.MoveNext
Loop
wfile.close
Wscript.echo "CSV file created"

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.